<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    unit: {
      type: String,
      default: ''
    },
    title: {
      type: String,
      default: '饼图'
    },
    project: {
      type: Array,
      default: function () {
        return [{
          name: "",
          value: 0
        }]
      }
    },
    data: {
      type: Array,
      default: function () {
        return [{
          name: "",
          value: 0
        }]
      }
    },
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    project: function (newV, oldV) {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    data: function (newV, oldV) {
      this.$nextTick(() => {
        this.initChart()
      })
    }
  },
  mounted() {

  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      let _this = this
      this.chart.setOption({
        title: {
          text: this.title,
          left: 'left'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10',
          // data: ['Industries', 'Technology', 'Forex', 'Gold', 'Forecasts']
          data: this.project
        },
        series: [
          {
            name: this.title,
            type: 'pie',
            roseType: 'radius',
            radius: ['20%', '50%'],
            center: ['50%', '38%'],
            data: this.data,
            label: {
              show: true, // 显示数值
              position: 'outside', // 数值显示在扇区外侧
              formatter: function (params) {
                if(params.percent === undefined ){
                  return params.name+":"+params.value+" "+_this.unit+" 0%"
                }else{
                  return params.name+":"+params.value+" "+_this.unit+" "+params.percent+"%"
                }
                // if (params) {
                //   return `${params.name}` + `\n\n${params.percent === undefined ? 0 : params.percent}%`
                // }
                // return ''
                //  return '{b}: {c}'+_this.unit+' ({d}%)' // 自定义标签的回调函数
              }
            },
            // data: [
            //   { value: 320, name: 'Industries' },
            //   { value: 240, name: 'Technology' },
            //   { value: 149, name: 'Forex' },
            //   { value: 100, name: 'Gold' },
            //   { value: 59, name: 'Forecasts' }
            // ],
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
